# -*- coding:utf-8 -*-

from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time


class Action():
    def __init__(self):
        # 初始化配置，设置Desired Capabilities参数
        self.desired_caps = {
            "platformName": "Android",
            "deviceName": "192.168.1.102:5555",
            "appPackage": "com.ss.android.ugc.aweme",
            "appActivity": "com.ss.android.ugc.aweme.main.MainActivity",
            "udid": "192.168.1.102:5555",
            "noReset": "True",
            "newCommandTimeout": "600",
            # "unicodeKeyboard": True,
            # "resetKeyboard": True

        }
        # 指定Appium Server
        self.server = 'http://localhost:4723/wd/hub'
        # 新建一个Session
        self.driver = webdriver.Remote(self.server, self.desired_caps)
        self.driver.implicitly_wait(10)
        time.sleep(5)
        #获取屏幕的高
        self.x = self.driver.get_window_size()['width']
        # 获取屏幕宽
        self.y = self.driver.get_window_size()['height']

    def comments(self):
        time.sleep(2)
        # app开启之后点击一次屏幕，确保页面的展示
        self.driver.tap([(300, 800)], 500)

    def scroll(self):
        # TouchAction(self.driver).tap(x=655, y=738).perform()
        # 无限滑动
        while True:
            TouchAction(self.driver).press(x=327, y=850).wait(300).move_to(x=349, y=578).release().perform()
            # time.sleep(5)
            # TouchAction(self.driver).press(x=467, y=957).wait(100).move_to(x=472, y=716).release().perform()

            # # 滑屏，大概从屏幕右边2分之一高度，往左侧滑动,滑动后显示的是 热点tab
            # self.driver.swipe(6/7*self.start_x, 1/2*self.start_y, 1/7*self.start_x, 1/2*self.start_y, 100)
            # #向右滑动，显示推荐tab 内容，第五个参数，时间设置大一点，否则容易看不到滑动效果
            # self.driver.swipe(1/7*self.start_x, 1/2*self.start_y, 5/7*self.start_x, 1/2*self.start_y, 200)
            #向上滑
            # self.driver.swipe(1 / 2 * self.x, 1 / 2 * self.y, 1 / 2 * self.x, 1 / 7 * self.y, 200)
            # # 向下滑动
            # self.driver.swipe(1/2*self.start_x, 1/7*self.start_y, 1/2*self.start_x, 6/7*self.start_y, 200)
            # 设置延时等待
            time.sleep(2)


    def send_content(self):
        TouchAction(self.driver).tap(x=845, y=92).perform()
        # TouchAction(self.driver).tap(x=42, y=95).perform()
        element = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located((By.ID, "com.ss.android.ugc.aweme:id/ahv")))
        element.send_keys('教育')

        # element1 = WebDriverWait(self.driver, 10).until(
        #     EC.presence_of_element_located((By.ID, "com.ss.android.ugc.aweme:id/css")))

        time.sleep(3)
        # 点击第一条
        TouchAction(self.driver).tap(x=251, y=202).perform()
        # time.sleep(1)
        # 点击用户
        # self.driver.find_elements_by_xpath('//*[@resource-id="android:id/text1"]')[2].click()
        WebDriverWait(self.driver, 5).until(
            EC.presence_of_element_located((By.XPATH, '//*[contains(@text, "用户")]'))).click()
        # 返回用户列表
        # userlist = WebDriverWait(self.driver, 5).until(
        #     EC.presence_of_all_elements_located((By.XPATH, '//*[@resource-id="com.ss.android.ugc.aweme:id/bo7"]/android.widget.LinearLayout')))

        while True:
            try:
                time.sleep(2)
                userlist = WebDriverWait(self.driver, 5).until(
                    # EC.presence_of_all_elements_located((By.XPATH, '//*[@resource-id="com.ss.android.ugc.aweme:id/bhv"]')))
                    EC.presence_of_all_elements_located((By.ID, "com.ss.android.ugc.aweme:id/bx3")))
                    # EC.presence_of_all_elements_located((By.ID, "com.ss.android.ugc.aweme:id/bwk")))
                for u in userlist:
                    # t = u.get_attribute("bounds")
                    # t= t.replace("[", "").split("]")
                    # x1 = int(t[0].split(",")[0])
                    # x2 = int(t[1].split(",")[0])
                    # y1 = int(t[0].split(",")[1])
                    # y2 = int(t[1].split(",")[1])
                    # self.driver.swipe(x2 - 300, y2 + 100, x1 + 300, y1 + 50, 200)
                    u.click()
                    # time.sleep(2)
                    WebDriverWait(self.driver, 5).until(
                        EC.presence_of_all_elements_located((By.ID, "com.ss.android.ugc.aweme:id/cml")))
                    self.driver.keyevent(4)  # 返回
                    # self.driver.swipe(327, 1250, 327, 279, 300)
                    # self.driver.swipe(1 / 2 * self.x, 1 / 2 * self.y, 1 / 2 * self.x, 1 / 7 * self.y, 200)
                self.swipeUp(self.driver)
                locator = ('class', 'android.widget.TextView')
                WebDriverWait(self.driver, 1).until(
                    EC.text_to_be_present_in_element(locator, "暂时没有更多了"))
                # WebDriverWait(self.driver, 1).until(
                #     EC.text_to_be_present_in_element((By.XPATH, '//android.widget.TextView[contains(@text, "暂时没有更多了")]')))
            except:
                pass



    def pinglun(self):
        while True:
            time.sleep(3)
            # 点击评论按钮
            element = WebDriverWait(self.driver, 3).until(
                EC.presence_of_element_located((By.XPATH, '//android.widget.ImageView[@resource-id="com.ss.android.ugc.aweme:id/yk"]')))
            self.driver.tap([(353, 179)])
            element.click()
            # 循环拉取内容
            while True:
                # 滚动拉取
                # self.driver.swipe(353, 1134, 353, 337, 100)
                TouchAction(self.driver).press(x=353, y=1134).wait(100).move_to(x=353, y=337).release().perform()
                # 判断是否还有评论
                # try:
                #     locator = ('class', 'android.widget.TextView')
                #     WebDriverWait(self.driver, 1).until(
                #         EC.text_to_be_present_in_element(locator, "暂时没有更多了"))
                #     # WebDriverWait(self.driver, 1).until(
                #     #     EC.presence_of_element_located((By.XPATH, '//android.widget.TextView[contains(@text, "暂时没有更多了")]')))
                # except:
                #     pass

            self.driver.keyevent(4) # 返回
            # 换下个视频
            TouchAction(self.driver).press(x=327, y=850).wait(300).move_to(x=349, y=578).release().perform()

    '''
    参数 1：driver
    参数 2：t 是持续时间
    参数 3：滑动次数
    '''
    def swipeUp(self, driver, t=500, n=1):
        '''向上滑动屏幕'''
        l = driver.get_window_size()
        x1 = l['width'] *  0.5  # x 坐标
        y1 = l['height'] * 0.75 # 起始 y 坐标
        y2 = l['height'] * 0.25 # 终点 y 坐标
        for i in range(n):
            driver.swipe(x1, y1, x1, y2, t)

    def swipeDown(self, driver, t=500, n=1):
        '''向下滑动屏幕'''
        l = driver.get_window_size()
        x1 = l['width']  * 0.5 # x 坐标
        y1 = l['height'] * 0.25 # 起始 y 坐标
        y2 = l['height'] * 0.75 # 终点 y 坐标
        for i in range(n):
            driver.swipe(x1, y1, x1, y2, 1)


    def swipLeft(self, driver, t=500, n=1):
        '''向左滑动屏幕'''
        l = driver.get_window_size()
        x1 = l['width']  * 0.75
        y1 = l['height'] * 0.5
        x2 = l['width']  * 0.05
        for i in range(n):
            driver.swipe(x1, y1, x2, y1, t)


    def swipRight(self, driver, t=500, n=1):
        '''向右滑动屏幕'''
        l = driver.get_window_size()
        x1 = l['width']  * 0.05
        y1 = l['height'] * 0.5
        x2 = l['width']  * 0.75
        for i in range(n):
            driver.swipe(x1, y1, x2, y1, t)


    def main(self):
        # self.comments()
        # self.scroll()
        self.send_content()
        # self.pinglun()



if __name__ == '__main__':
    action = Action()
    action.main()
